﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNH.MyLayouts;
using System.Threading;
using QLNH.QLNH_BUS_WS;

namespace QLNH.MyForms
{
    public partial class AdminFavoriteFoodsStatistic : FormUC
    {
        public AdminFavoriteFoodsStatistic(LayoutUC mainLayout)
        {
            InitializeComponent();
            _mainLayout = mainLayout;
            _formKey = FormKey.ADMIN_FAVORITE_FOODS_STATISTIC;
        }

        private void getAllCatalog()
        {
            Thread t = new Thread(new ThreadStart(getAllCatalogThread));
            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;
            t.Start();
        }

        private void getAllCatalogThread()
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            CatalogDTO[] catalogues = ws.GetAllCatalog();

            List<CatalogDTO> ds = new List<CatalogDTO>();
            ds.Add(new CatalogDTO()
            {
                Name = "---Tất cả---",
                ID = ""
            });
            ds.AddRange(catalogues.ToList<CatalogDTO>());

            cb_catalogues.BeginInvoke((MethodInvoker)(() =>
                {
                    cb_catalogues.DataSource = ds;
                    cb_catalogues.DisplayMember = "Name";
                    cb_catalogues.ValueMember = "ID";
                }));
        }

        private int choose = 0;

        private void rbt_checkedChanged(object sender, EventArgs e)
        {
            RadioButton control = (RadioButton)sender;

            if (control != null)
            {
                int tag = Int32.Parse(control.Tag.ToString());
                choose = tag;
                switch (tag)
                {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        dtp_start.Enabled = dtp_end.Enabled = rbt_date.Checked;
                        break;
                    default: break;
                }
            }
        }

        private void AdminFavoriteFoodsStatistic_Load(object sender, EventArgs e)
        {
            getAllCatalog();
        }

        private void bt_back_Click(object sender, EventArgs e)
        {
            _mainLayout.SwitchForm(FormKey.ADMIN_STATISTICS);
        }

        private void bt_viewStatistic_Click(object sender, EventArgs e)
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();

            FoodReportDTO[] foods = null;

            DateTime from, to;
            DateTime now = DateTime.Now;
            from = to = now;

            switch (choose)
            {
                case 0:
                    break;
                case 1:
                    {
                        int dayInWeek = now.DayOfWeek - DayOfWeek.Sunday;
                        from = DateTime.Now.AddDays(-dayInWeek);
                        to = now;
                    }
                    break;
                case 2:
                    {
                        from = DateTime.Now.AddDays(-now.Day + 1);
                        to = now;
                    }
                    break;
                case 3:
                    {
                        from = dtp_start.Value;
                        to = dtp_end.Value;
                    }
                    break;
                default: break;
            }

            string catalogID = cb_catalogues.SelectedValue.ToString();
            if (choose == 0)
            {
                foods = ws.GetCalledMeals(catalogID);
            }
            else
            {
                foods = ws.GetCalledMealsByDate(from, to, catalogID);
            }

            FoodReportDTOBindingSource.DataSource = foods;
            reportViewer.RefreshReport();
        }
    }
}
